# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.10)

project(ecmastdlib)

add_panda_assembly(TARGET ecmastdlib SOURCE ecmastdlib.pa)

if(CMAKE_CROSSCOMPILING)
    ExternalProject_Get_Property(panda_host_tools binary_dir)
    set(arkquick_target panda_host_tools-build)
    set(arkquick_bin    "${binary_dir}/quickener/arkquick")
else()
    set(arkquick_target arkquick)
    set(arkquick_bin    $<TARGET_FILE:${arkquick_target}>)
endif()

if (PANDA_WITH_QUICKENER)
    add_custom_command(TARGET ecmastdlib
                       POST_BUILD
                       COMMAND ${arkquick_bin} "${CMAKE_CURRENT_BINARY_DIR}/ecmastdlib.abc" "${CMAKE_CURRENT_BINARY_DIR}/ecmastdlib.abcq"
                       WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                       DEPENDS arkquick)
endif()

set(ECMASTDLIB_INLINE_GEN "${CMAKE_CURRENT_BINARY_DIR}/ecmastdlib_inline_gen.h")
set(ECMASTDLIB_INLINE_GEN_RB "${CMAKE_CURRENT_SOURCE_DIR}/ecmastdlib_inline_gen.rb")
set(ECMASTDLIB_INLINE_GEN_ERB "${CMAKE_CURRENT_SOURCE_DIR}/ecmastdlib_inline_gen.h.erb")

add_custom_command(OUTPUT "${ECMASTDLIB_INLINE_GEN}"
                   COMMENT "Generate inlined header with ecmastdlib"
                   COMMAND "${ECMASTDLIB_INLINE_GEN_RB}" -t "${ECMASTDLIB_INLINE_GEN_ERB}" -d ${CMAKE_CURRENT_BINARY_DIR}/ecmastdlib.abc -o "${ECMASTDLIB_INLINE_GEN}"
                   DEPENDS "${ECMASTDLIB_INLINE_GEN_RB}" "${ECMASTDLIB_INLINE_GEN_ERB}" ecmastdlib)

add_custom_target(ecmastdlib_inline_h DEPENDS "${ECMASTDLIB_INLINE_GEN}")

add_custom_target(ecmastdlib_inline_h_empty
                  COMMENT "Generate empty inlined header for static analyzer"
                  COMMAND "${ECMASTDLIB_INLINE_GEN_RB}" -t "${ECMASTDLIB_INLINE_GEN_ERB}" -o "${ECMASTDLIB_INLINE_GEN}"
                  DEPENDS "${ECMASTDLIB_INLINE_GEN_RB}" "${ECMASTDLIB_INLINE_GEN_ERB}")

add_dependencies(panda_gen_files ecmastdlib_inline_h_empty)
